package com.ledi.guns.core.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密类（封装jdk自带的md5加密方法）
 *
 * @author fengshuonan
 * @date 2016年12月2日 下午4:14:22
 */
public class MD5Util {

	public static String encrypt(String source) {

		return encodeMd5(source.getBytes());
	}

	private static String encodeMd5(byte[] source) {

		try {
			return encodeHex(MessageDigest.getInstance("MD5").digest(source));
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException(e.getMessage(), e);
		}
	}

	private static String encodeHex(byte[] bytes) {

		StringBuffer buffer = new StringBuffer(bytes.length * 2);
		for (byte b : bytes) {
			if ((b & 0xff) < 0x10) {
				buffer.append("0");
			}
			buffer.append(Long.toString(b & 0xff, 16));
		}
		return buffer.toString();
	}

	public static void main(String[] args) {

		System.out.println(encrypt("123456"));
	}

	private static String byteArrayToHexString(byte b[]) {

		StringBuffer resultSb = new StringBuffer();
		for (byte element : b) {
			resultSb.append(byteToHexString(element));
		}

		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {

		int n = b;
		if (n < 0) {
			n += 256;
		}
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String MD5Encode(String origin, String charsetname) {

		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname)) {
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			} else {
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
			}
		} catch (Exception exception) {
		}
		return resultString;
	}

	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}
